﻿using UnityEditor;
using UnityEngine;
using UnityEngine.UI;

namespace Game.Editor
{
    public class GameUICanvasUtils
    {
        [MenuItem("工具/UI画布", priority = 2050)]
        static void BuildUICanvas()
        {
            InitCanvas();
        }
        
        public static void InitCanvas()
        {
            GameObject goGamera = new GameObject("UICamera", typeof(Camera));
            Camera camera = goGamera.GetComponent<Camera>();
            camera.clearFlags = CameraClearFlags.Depth;
            camera.backgroundColor = Color.black;
            camera.depth = 0;
            camera.orthographic = true;
            
            GameObject gameObject = new GameObject("Canvas", typeof(RectTransform));
            RectTransform transform = gameObject.transform as RectTransform;
            Canvas canvas = gameObject.AddComponent<Canvas>();
            canvas.renderMode = RenderMode.ScreenSpaceCamera;
            canvas.worldCamera = camera;
            // canvas.additionalShaderChannels = AdditionalCanvasShaderChannels.Normal | 
            //                                   AdditionalCanvasShaderChannels.Tangent | 
            //                                   AdditionalCanvasShaderChannels.TexCoord1 |
            //                                   AdditionalCanvasShaderChannels.TexCoord2 |
            //                                   AdditionalCanvasShaderChannels.TexCoord3;
            CanvasScaler scaler = gameObject.AddComponent<CanvasScaler>();
            scaler.screenMatchMode = CanvasScaler.ScreenMatchMode.MatchWidthOrHeight;
            scaler.uiScaleMode = CanvasScaler.ScaleMode.ScaleWithScreenSize;
            scaler.referenceResolution = Const.PreferResolution;
            scaler.matchWidthOrHeight = 0.5f;
            gameObject.AddComponent<GraphicRaycaster>();
        }
    }
}